﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wp.DAL;
using Wp.BLL;
using System.Data;
using Tools;
using System.IO;
using HtmlAgilityPack;

public partial class GetDateOld : System.Web.UI.Page
{
    public List<string> imgScrs = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //CreatePost();
            CreateAlbum();
        }
    }
    void CreateAlbum()
    {
        string querystring = @"select  *
                from [dbo].[TblAlbum] where IsVisible=1";
        DataTable dt = SqlHelper.ExecuteDataTable("Server=suoinguontinhthuong.vn;Database=chuaphuocquang;uid=chuaphuocquang;pwd=123456789*&#;", CommandType.Text, querystring, null);
        for (int k = 0; k < dt.Rows.Count; k++)
        {
            DataRow r = dt.Rows[k];
            TblAlbumEntity album = GetTblAlbumEntity(r);
            int _Id = TblAlbumManager.CreateInstant().InsertUpdate(album);
            if (_Id > 0)
            {
                album.Id = _Id;
                string querystringphoto = @"select  * from [dbo].[TblPhoto] where AlbumId='" + r["Id"].ToString() + "'";
                DataTable dtphoto = SqlHelper.ExecuteDataTable("Server=suoinguontinhthuong.vn;Database=chuaphuocquang;uid=chuaphuocquang;pwd=123456789*&#;", CommandType.Text, querystringphoto, null);
                if (dtphoto != null)
                {
                    foreach (DataRow item in dtphoto.Rows)
                    {
                        try
                        {
                            TblPhotoManager.CreateInstant().InsertUpdate(GetTblPhotoEntity(item, _Id));
                        }
                        catch (Exception)
                        {
                            
                        }
                    }
                }

            }
            //getstring(r["Content"].ToString());
        }
    }
    void CreatePost()
    {
        string querystring = @"select  *
                from [dbo].[News] where IsVisible=1 And CreatedDate >= '2012-1-1'";
        //*,CASE When CategoryTextId='phat-giao-quoc-te' Then 20 
        //        When CategoryTextId='van-hoc-phat-giao' Then 24
        //         When CategoryTextId='trung-tam-co-nhi-vien' Then 2
        //         When CategoryTextId='phat-hoc' Then 16
        //        When CategoryTextId='tu-thien' Then 11
        //         When CategoryTextId='sinh-hoat-noi-tu' Then 10 
        //         When CategoryTextId='thoi-su' Then 6
        //         When CategoryTextId='phat-giao-va-doi-song' Then 5 
        //         When CategoryTextId='hoang-phap' Then 12  
        //         When CategoryTextId='van-hoa-phat-giao' Then 7  End CatID

        //And CategoryTextId IN  ('phat-giao-quoc-te',
        //                                                                'van-hoc-phat-giao',
        //                                                                'trung-tam-co-nhi-vien',
        //                                                                'phat-hoc',
        //                                                                'tu-thien',
        //                                                                'sinh-hoat-noi-tu',
        //                                                                'thoi-su',
        //                                                                'phat-giao-va-doi-song',
        //                                                                'hoang-phap',
        //                                                                'van-hoa-phat-giao'
        //                                                                )
        DataTable dt = SqlHelper.ExecuteDataTable("Server=suoinguontinhthuong.vn;Database=chuaphuocquang;uid=chuaphuocquang;pwd=123456789*&#;", CommandType.Text, querystring, null);
        for (int k = 0; k < dt.Rows.Count; k++)
        {
            DataRow r = dt.Rows[k];
            getstring(r["Content"].ToString());

            //int _Id = TblPostsManager.CreateInstant().InsertUpdate(GetTblPostsEntity(r));
            //if (_Id != -1)
            //{
            //    TblTermRelationshipsManager.CreateInstant().InsertUpdate(_Id, r["CatID"].ToInt());
            //    // Update(r["Id"].ToInt(), r["CatID"].ToInt());//update category
            //    //string[] str = "Phước Quang,Suối Nguồn Tình Thương".Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            //    //TblTermRelationshipsManager.CreateInstant().DeleteByObjectIdAndTaxonomy(_Id, "posttags");
            //    //for (int i = 0; i < str.Length; i++)
            //    //{
            //    //    TblTermTaxonomyManager.CreateInstant().InsertPostTag(_Id, str[i], "posttags");
            //    //}
            //    //List<TblPostMetaEntity> lsmeta = TblPostMetaManager.CreateInstant().SelectByPostId(_Id);
            //    //if (lsmeta != null)
            //    //{
            //    //TblPostMetaEntity itemnumview = lsmeta.Where(n => n.MetaKey.Equals("NumView")).SingleOrDefault();
            //    //if (itemnumview == null)
            //    //{
            //    TblPostMetaManager.CreateInstant().InsertUpdate(-1, _Id, "NumView", r["NumViews"].ToString());
            //    //}
            //    //TblPostMetaEntity itemtitle = lsmeta.Where(n => n.MetaKey.Equals("SEOTitle")).SingleOrDefault();
            //    //if (itemtitle != null)
            //    //{
            //    //  itemtitle.MetaValue = r["Title"].ToString();
            //    //    TblPostMetaManager.CreateInstant().InsertUpdate(itemtitle);
            //    //}
            //    //else if (!txtSEOTitle.Text.Equals(""))
            //    //{
            //    TblPostMetaManager.CreateInstant().InsertUpdate(-1, _Id, "SEOTitle", r["Title"].ToString());
            //    //}

            //    //TblPostMetaEntity itemkeyword = lsmeta.Where(n => n.MetaKey.Equals("SEOKeyWord")).SingleOrDefault();
            //    //if (itemkeyword != null)
            //    //{
            //    //    itemkeyword.MetaValue = txtSEOKeyWord.Text;
            //    //    TblPostMetaManager.CreateInstant().InsertUpdate(itemtitle);
            //    //}
            //    //else if (!txtSEODescription.Text.Equals(""))
            //    //{
            //    //    TblPostMetaManager.CreateInstant().InsertUpdate(-1, _Id, "SEODescription", txtSEODescription.Text);
            //    //}
            //    //TblPostMetaEntity itemdescription = lsmeta.Where(n => n.MetaKey.Equals("SEODescription")).SingleOrDefault();
            //    //if (itemdescription != null)
            //    //{
            //    //    itemdescription.MetaValue = txtSEODescription.Text;
            //    //    TblPostMetaManager.CreateInstant().InsertUpdate(itemdescription);
            //    //}
            //    //else if (!txtSEOKeyWord.Text.Equals(""))
            //    //{
            //    //    TblPostMetaManager.CreateInstant().InsertUpdate(-1, _Id, "SEOKeyWord", txtSEOKeyWord.Text);
            //    //}
            //    //}
            //}
        }
        System.IO.File.WriteAllLines(@"E:\Works\Web\ChuaPhuocQuang\SourceCode\imagefilemp3_1.txt", imgScrs.ToArray());
    }
    //public void Update(int PostId, int TermTaxonomyId)
    //{
    //    DataTable dt = new DataTable();
    //    DataColumn column = new DataColumn();
    //    column.DataType = System.Type.GetType("System.Int32");
    //    column.AllowDBNull = false;
    //    column.Caption = "ObjectId";
    //    column.ColumnName = "ObjectId";
    //    dt.Columns.Add(column);
    //    column = new DataColumn();
    //    column.DataType = System.Type.GetType("System.Int32");
    //    column.AllowDBNull = false;
    //    column.Caption = "TermTaxonomyId";
    //    column.ColumnName = "TermTaxonomyId";
    //    dt.Columns.Add(column);
    //    //bool ChangeValue = false;
    //    //for (int i = 0; i < rpt.Items.Count; i++)
    //    //{
    //    //    CheckBox cb = (CheckBox)rpt.Items[i].FindControl("cb");
    //    //    HiddenField hdf = (HiddenField)rpt.Items[i].FindControl("hdf");
    //    //    if (cb.Checked)
    //    //    {
    //    DataRow r = dt.NewRow();
    //    r["ObjectId"] = PostId;
    //    r["TermTaxonomyId"] = TermTaxonomyId;
    //    dt.Rows.Add(r);
    //    // ChangeValue = true;
    //    //    }
    //    //}
    //    dt.AcceptChanges();
    //    //if (ChangeValue)
    //    TblTermRelationshipsManager.CreateInstant().Update(dt);
    //}
    TblPostsEntity GetTblPostsEntity(DataRow r)
    {
        TblPostsEntity item = new TblPostsEntity();
        item.TextId = TblPostsManager.CreateInstant().GetUniqueTextIdFromUnicodeText(r["Title"].ToString());
        item.PostType = "post";
        item.Url = "";
        item.CreatedBy = HttpContext.Current.User.Identity.Name;
        item.CreatedDate = DateTime.Now;
        item.DisplayOrder = 1;
        item.ParentId = -1;
        item.CommentStatus = "allow";//prohibit
        item.PublishDate = DateTime.Now;

        item.Title = r["Title"].ToString();
        item.Summary = UnicodeConversion.CreateInstant().GetTextFromHtml(r["Summary"].ToString());
        item.Description = r["Content"].ToString().Replace("UserFiles/image", "static/2013/C07");
        string[] file = r["UrlImage"].ToString().Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
        if (file.Length > 0)
        {
            item.ImageUrl = "/static/2013/07/" + file[file.Length - 1];
        }
        item.Author = HttpContext.Current.User.Identity.Name;
        item.ModifiedBy = HttpContext.Current.User.Identity.Name;
        item.PostStatus = "publish";
        item.ModifiedDate = DateTime.Now;
        return item;
    }
    TblAlbumEntity GetTblAlbumEntity(DataRow r)
    {
        TblAlbumEntity item = new TblAlbumEntity();
        item.TextId = TblAlbumManager.CreateInstant().GetUniqueTextIdFromUnicodeText(r["Name"].ToString());
        item.CreatedBy = HttpContext.Current.User.Identity.Name;
        item.CreatedDate = DateTime.Now;
        item.DisplayOrder = 1;
        item.Name = r["Name"].ToString();
        string[] file = r["UrlImage"].ToString().Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
        if (file.Length > 0)
        {
            item.ImageUrl = "/static/2013/07/" + file[file.Length - 1];
        }
        item.ModifiedBy = HttpContext.Current.User.Identity.Name;
        item.ModifiedDate = DateTime.Now;
        item.IsVisible = true;
        return item;
    }
    TblPhotoEntity GetTblPhotoEntity(DataRow r, int AlbumId)
    {
        TblPhotoEntity item = new TblPhotoEntity();
        item.TextId = TblPhotoManager.CreateInstant().GetUniqueTextIdFromUnicodeText(r["Name"].ToString());
        item.CreatedBy = HttpContext.Current.User.Identity.Name;
        item.CreatedDate = DateTime.Now;
        item.DisplayOrder = 1;
        item.AlbumId = AlbumId;
        item.Name = r["Name"].ToString();
        string[] file = r["UrlImage"].ToString().Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
        if (file.Length > 0)
        {
            item.ImageUrl = "/static/album/" + file[file.Length - 2] + "/" + file[file.Length - 1];
        }
        item.ModifiedBy = HttpContext.Current.User.Identity.Name;
        item.ModifiedDate = DateTime.Now;
        return item;
    }
    public void getstring(string content)
    {
        //HtmlDocument doc = new HtmlDocument();
        //doc.LoadHtml(content);//or doc.Load(htmlFileStream)
        //var nodes = doc.DocumentNode.SelectNodes(@"//img[@src]");
        //if (nodes != null)
        //    foreach (var img in nodes)
        //    {
        //        HtmlAttribute att = img.Attributes["src"];
        //        if (!att.Value.Contains("http"))
        //        {
        //            imgScrs.Add("," + att.Value);
        //        }

        //    }
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(content);//or doc.Load(htmlFileStream)
        var nodes = doc.DocumentNode.SelectNodes(@"//a");
        if (nodes != null)
            foreach (var img in nodes)
            {
                HtmlAttribute att = img.Attributes["href"];
                if (att != null)
                    if (att.Value.ToLower().Contains(".mp3") || att.Value.Contains(".pps"))
                    {
                        imgScrs.Add("," + att.Value);
                    }

            }


    }

}